﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESOL.Common.Entity;
using Suzsoft.Smart.Data;
using System.Data;

namespace ESOL.Business.Hr
{
    /// <summary>
    /// HrEmployeeIoRecord
    /// </summary>
    public partial class HrEmployeeIoRecordBs
    {
        #region GetByID
        /// <summary>
        /// Get Entity
        /// </summary>
        /// <param name="objHrEmployeeIoRecordEntity">Entity</param>
        /// <returns>Entity</returns>
        public static HrEmployeeIoRecordEntity GetHrEmployeeIoRecord(HrEmployeeIoRecordEntity objHrEmployeeIoRecordEntity)
        {
            return DataAccess.SelectSingle<HrEmployeeIoRecordEntity>(objHrEmployeeIoRecordEntity);
        }
        #endregion


        #region Insert
        /// <summary>
        /// Insert Data
        /// </summary>
        /// <param name="objHrEmployeeIoRecordEntity">Entity</param>
        /// <returns>true Insert Succeed; false Insert Fail</returns>
        public static bool InsertHrEmployeeIoRecord(HrEmployeeIoRecordEntity objHrEmployeeIoRecordEntity)
        {
            return DataAccess.Insert(objHrEmployeeIoRecordEntity);
        }

        public static bool InsertHrEmployeeIoRecord(List<HrEmployeeIoRecordEntity> objHrEmployeeIoRecordEntityList)
        {
            return DataAccess.Insert(objHrEmployeeIoRecordEntityList);
        }
        #endregion


        #region Update
        /// <summary>
        /// Update Data
        /// </summary>
        /// <param name="objHrEmployeeIoRecordEntity">Entity</param>
        /// <returns>true Update Succeed; false Update Fail</returns>
        public static bool UpdateHrEmployeeIoRecord(HrEmployeeIoRecordEntity objHrEmployeeIoRecordEntity)
        {
            return DataAccess.Update(objHrEmployeeIoRecordEntity);
        }

        public static bool UpdateHrEmployeeIoRecord(List<HrEmployeeIoRecordEntity> objHrEmployeeIoRecordEntityList)
        {
            return DataAccess.Update<HrEmployeeIoRecordEntity>(objHrEmployeeIoRecordEntityList);
        }
        #endregion


        #region Delete
        /// <summary>
        /// Delete Data
        /// </summary>
        /// <param name="objHrEmployeeIoRecordEntity">Entity</param>
        /// <returns>true Delete Succeed; false Delete Fail</returns>
        public static bool DeleteHrEmployeeIoRecord(HrEmployeeIoRecordEntity objHrEmployeeIoRecordEntity)
        {
            return DataAccess.Delete(objHrEmployeeIoRecordEntity);
        }

        public static bool DeleteHrEmployeeIoRecord(List<HrEmployeeIoRecordEntity> objHrEmployeeIoRecordEntityList)
        {
            return DataAccess.Delete<HrEmployeeIoRecordEntity>(objHrEmployeeIoRecordEntityList);
        }
        #endregion


        #region GetAll
        /// <summary>
        /// Get All Data
        /// </summary>
        /// <returns>Back All Data<returns>
        public static DataSet GetHrEmployeeIoRecordAll()
        {
            return DataAccess.SelectAllDataSet<HrEmployeeIoRecordEntity>();
        }
        /// <summary>
        /// Get All Data List
        /// </summary>
        /// <returns>Back All Data List<returns>
        public static List<HrEmployeeIoRecordEntity> GetHrEmployeeIoRecordAllList()
        {
            return DataAccess.SelectAll<HrEmployeeIoRecordEntity>();
        }
        #endregion


        public static DataTable GetHrEmployeeIoRecordByCondition(DTOHrEmployeeIoRecordEntity condition)
        {
            string sql = @"Select {0} IO.*, P.EMPLOYEE_NAME , P.DEPARTMENT_NO,P.POSITION_ID
                                FROM  HR_EMPLOYEE_IO_RECORD IO
                                LEFT JOIN hr_employee P ON P.Employee_NO = IO.EMPLOYEE_NO
                                WHERE 1=1 ";

            if (condition != null && condition.result!=null)
            {
                sql = string.Format(sql, condition.rowCount > 0 ? " top " + condition.rowCount : "");
                HrEmployeeIoRecordEntity c= condition.result;

                if(!string.IsNullOrEmpty(c.Id))
                    sql += " AND IO.ID = '" + c.Id + "'";
                if (!string.IsNullOrEmpty(c.ReqNo))
                    sql += " AND IO.REQ_NO LIKE '%" + c.ReqNo + "%'";
                if (!string.IsNullOrEmpty(c.DepartmentNo))
                    sql += " AND IO.DEPARTMENT_NO = '" + c.DepartmentNo + "'";
                if (!string.IsNullOrEmpty(c.EmployeeNo))
                    sql += " AND IO.EMPLOYEE_NO  LIKE '%" + c.EmployeeNo + "%'";
                if (!string.IsNullOrEmpty(c.Status))
                    sql += " AND IO.STATUS = '" + c.Status + "'";

                if (c.OutDate > DateTime.MinValue)
                {
                    sql += " AND io.OUT_DATE >= CONVERT(DATETIME,'" + c.OutDate.ToString("yyyy-MM-dd") + " 00:00:00')";
                }

            }

            sql += " ORDER BY CREATED_DATE DESC";

            DataSet ds = DataAccess.SelectDataSet(sql);
            if (ds != null && ds.Tables.Count > 0)
            {
                return ds.Tables[0];
            }
            return null;

        }

        


    }
}
